package com.wjj.sixinone.config;


import com.wjj.sixinone.common.enums.ApiTypeConst;
import com.wjj.sixinone.interceptor.IpAddressInterceptor;
import com.wjj.sixinone.interceptor.LoginAuthInterceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.*;

/**
 * 在resource里面引入静态资源记得做静态资源映射
 * 如addResourceHandler里面添加 /layui/**
 * addResourceLocations加 "classpath:/static/layui/",
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    @Autowired
    private LoginAuthInterceptor loginAuthInterceptor;
    @Autowired
    private IpAddressInterceptor ipAddressInterceptor;

    //    拦截器注册
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginAuthInterceptor)

                .excludePathPatterns(ApiTypeConst.API_PUBLIC + "/**",
                        "/image/**",
                        "/swagger-ui/**", "/v3/api-docs/**", "/doc.html", "/error/**",
                        "/webjars/**", "/index.html", "/page/**", "/layui/**","/jquery/**","/util/**","/echarts/**")
               .addPathPatterns("/**");
        registry.addInterceptor(ipAddressInterceptor)
                .addPathPatterns("/**");
    }

    private CorsConfiguration corsConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setMaxAge(3600L);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfig());
        return new CorsFilter(source);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/index.html", "/login.html", "/layui/**","/jquery/**","/util/**","/page/**","/echarts/**","/image/**")
                .addResourceLocations("classpath:/static/",
                        "classpath:/META-INF/resources/",
                        "classpath:/resources/",
                        "classpath:/public/",
                        "classpath:/static/page/",
                        "classpath:/static/layui/",
                        "classpath:/static/jquery/",
                        "classpath:/static/util/",
                        "classpath:/static/echarts/",
                        "classpath:/static/image/",



                        "classpath:/");
        registry.addResourceHandler("/doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }


}
